/*
 * @Author: 码农 2646995216@qq.com
 * @Date: 2025-03-17 14:06:51
 * @LastEditors: 码农 2646995216@qq.com
 * @LastEditTime: 2025-03-17 15:26:36
 * @FilePath: /socket_tcp/Task.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#pragma once
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <string>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include<functional>

using namespace std;

class ThreadData
{
public:
	ThreadData(int fd) : sockfd(fd)
	{
	}

	void run()
	{
		char buffer[1024]; // 定义缓冲区，用于存放客户端发来的数据
		while (true)
		{
			size_t n = read(sockfd, buffer, sizeof(buffer) - 1); // 减一是为了留出一个\0
			if (n > 0)
			{
				buffer[n] = '\0'; // 把发来的\n覆盖掉

				cout << "Server get a data: " << buffer << endl;
				string out;
				out += buffer;
				write(sockfd, out.c_str(), out.size()); // 将数据写回
			}
			else
			{
				cout << "Client quit" << endl;
				break;
			}
		}
		
	}

public:
	int sockfd;
};
